<?php

/**
 * @author ducnhat
 * @email ddnhat@gmail.com
 * @copyright 2014
 */
 
class Admin_BuyTotalController extends Zenddn_Controller_Action{
	
	public function init(){
		parent::init();
        $this->loadTemplate('alpona');
        $this->view->headTitle = "Mua bán hàng ngày";
	}
	
	public function indexAction(){
		$scripts = array('jquery.dataTables.js', 'dataTables.bootstrap.js', 'TableTools.js', 'jquery-ui-1.10.3.custom.min.js', 'autoNumeric.js');
		parent::addScripts($scripts);
        parent::addStyleSheet(array('jquery-ui-1.10.3.custom.css'));
        
        $request = $this->_request;
        
        $customer_model = new Admin_Model_Customer();
        $customers = $customer_model->getDatas();
        
        $supplier_model = new Admin_Model_Supplier();
        $suppliers = $supplier_model->getDatas();
        
        $model = new Admin_Model_BuyTotal();
        $data = $model->getDatas();
        
        $this->view->data = $data;
        $this->view->customers = $customers;
        $this->view->suppliers = $suppliers;
	}
    
    public function addAction(){
        $request = $this->_request;
        
        if($request->isXmlHttpRequest()){
            $data = $request->getParams();
            
            unset($data['module']);
            unset($data['controller']);
            unset($data['action']);
            
            $date = DateTime::createFromFormat('d-m-Y', $data['date']);
            $data['date'] = $date->format("Y-m-d");
            
            $model = new Admin_Model_BuyTotal();
            $result = $model->saveData($data);
            
            echo $result;
        }
        
        $this->_helper->viewRenderer->setNoRender();
        $this->_helper->layout->disableLayout();
    }
    
    public function getDataAction(){
        $request = $this->_request;
        
        if($request->isXmlHttpRequest()){
            $id = $request->getParam('id', false);
            
            $result['error'] = true;
            
            if($id != false){
                $model = new Admin_Model_BuyTotal();
                $rs = $model->getData($id);
                
                if($rs != null){
                    $result['data'] = $rs;
                    $result['error'] = false;
                }
            }
            
            echo json_encode($result);
        }
        
        $this->_helper->viewRenderer->setNoRender();
        $this->_helper->layout->disableLayout();
    }
    
    public function getDeptAction(){
        $request = $this->_request;
        
        if($request->isXmlHttpRequest()){
            $customer_id = $request->getParam('customer_id', false);
            $date = $request->getParam('date', false);
            $id = $request->getParam('order_id', false);
            
            if($id == false){
                $id = null;
            }
            
            $result = 0;
            
            if($customer_id != false && $date != false){
                $date = DateTime::createFromFormat('d-m-Y', $date);
                $date = $date->format("Y-m-d");
                
                $model = new Admin_Model_BuyTotal();
                $dept = $model->getDept($customer_id, $date, $id);
                
                if($dept != null){
                    $result = $dept;
                }
            }
            
            echo $result;
        }
        
        $this->_helper->viewRenderer->setNoRender();
        $this->_helper->layout->disableLayout();
    }
    
    public function deleteAction(){
        $request = $this->_request;
        
        if($request->isXmlHttpRequest()){
            $id = $request->getParam('id', false);
            
            if($id != false){
                $model = new Admin_Model_BuyTotal();
                $result = $model->deleteData($id);
                
                echo $result;
            }
            
            echo false;
        }
        
        $this->_helper->viewRenderer->setNoRender();
        $this->_helper->layout->disableLayout();
    }
}